import { getQuestionListService } from '../services/question'
import { useRequest } from 'ahooks'
import { useSearchParams } from 'react-router-dom'
import { 
    SEARCH_LIST_KEYWORD,
    SEARCH_PAGE_SIZE_PARAMS_KEY,
    SEARCH_PAGE_PARAMS_KEY,
    LIST_PAGE_SIZE
 } from '../constant'
type listParams = {
    isStar:boolean
    isDeleted:boolean
}
const useLoadQuestionListData = (opt:Partial<listParams>) => {
    const {isStar,isDeleted} = opt
    const [ searchParams ] = useSearchParams()
    console.log('searchParams',searchParams)
    // searchParams.get('keyword')
        // 当刷新的依赖项 searchParams 改变时 就会重新执行useRequest
        const {data,loading,error} = useRequest(  async () => {
            const keyWord = searchParams.get(SEARCH_LIST_KEYWORD) || ''
            const page = parseInt(searchParams.get(SEARCH_PAGE_PARAMS_KEY) || '') || 1
            const pageSize = parseInt(searchParams.get(SEARCH_PAGE_SIZE_PARAMS_KEY) || '') || LIST_PAGE_SIZE
        
            const data = await getQuestionListService({ keyWord,isStar,isDeleted,page,pageSize })
            return data
        },
        {
            refreshDeps:[ searchParams ]
        })
        return {data,loading,error}
}

export default useLoadQuestionListData
